package com.zlx.dao;

import com.zlx.entities.Meeting;
import com.zlx.utils.Page;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author 周罗校
 * @Description 会议模块的dao操作
 * @ClassName MeetingDao
 * @date 2020.09.25 10:44:12
 * 该版权归周罗校所有，最终解释权以官方为准
 */
public class MeetingDao extends BaseDao{
    /**
     * @Author 周罗校
     * @Description 分页、模糊查询和显示页面数据
     * @Param [page, name]
     * @return java.util.List<com.zlx.entities.Meeting>
     * @Date 2020.09.25 16:56:09
     **/
    public List<Meeting> listAll(Page<Meeting> page, String name){
        String sql = "SELECT\n" +
                "m.*,\n" +
                "d.`name` AS deptName \n" +
                "FROM\n" +
                "`meeting` m\n" +
                "LEFT JOIN dept d ON m.dept_id = d.id WHERE title LIKE ? LIMIT ?,?";
        try {
            List<Meeting> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Meeting.class),"%"+name+"%",page.getStart(),page.getPageSize());
            return list;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * @Author 周罗校
     * @Description 获取记录数
     * @Param [name]
     * @return java.lang.Integer
     * @Date 2020.09.25 16:56:42
     **/
    public Integer getMeetCount(String name){
        String sql = "select count(*)  from meeting where title like ?";
        return jdbcTemplate.queryForObject(sql, Integer.class, "%"+name+"%");
    }
    public void InsertMeeting(Meeting meeting){
        String sql = "INSERT INTO `myxbhy`.`meeting` ( `id`, `dept_id`, `title`, `content`, `publish_date`, `start_time`, `end_time`, `status`, `make_user` )\n" +
                "values (null,?,?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,
                meeting.getDeptId(),
                meeting.getTitle(),
                meeting.getContent(),
                new Date(),
                meeting.getStartTime(),
                meeting.getEndTime(),
                meeting.getStatus(),
                Arrays.toString(meeting.getMakeUsers()));
        System.out.println("sfaf");
    }
    public void updateStatusById(Integer status,Integer id){
        String sql = "update meeting set status = ? where id = ?";
        jdbcTemplate.update(sql,status,id);
    }
    public List<Meeting> getAllInfoRToUpdateStatus(){
        String sql = "SELECT id,start_time,end_time,`status` FROM meeting";
        List<Meeting> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Meeting.class));
        return query;
    }
    public Meeting getMeetingInfoById(Integer id){
        String sql = "SELECT\n" +
                "m.*,\n" +
                "d.`name` AS deptName \n" +
                "FROM\n" +
                "`meeting` m\n" +
                "LEFT JOIN dept d ON m.dept_id = d.id WHERE m.id = ?";
        try {
            Meeting meeting = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Meeting.class), id);
            return meeting;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
    public void insertJoinMeetingInfo(Integer uid,Integer mid){
        String sql ="insert into meeting_join values(?,?)";
        jdbcTemplate.update(sql,uid,mid);
    }
    public Integer getJoin(Integer uid,Integer mid){
        String sql = "select count(*) as count from meeting_join where u_id = ? and c_id = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class,uid,mid);
    }
    public void deleteJoinMeeting(Integer uid,Integer mid){
        String sql = "delete from meeting_join where u_id = ? and c_id = ?";
        jdbcTemplate.update(sql,uid,mid);
    }
    public Integer getJoinMeetinngCount(Integer mid){
        String sql = "select count(*) as count from meeting_join where c_id = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class,mid);
    }
    public void deleteMeetinngById(Integer id){
        String sql = "delete from meeting where id =  ?";
        jdbcTemplate.update(sql,id);
    }
}
